package han.chensing.latos.maker.latos.file.util;

import com.badlogic.gdx.utils.OrderedSet;
import han.chensing.latos.common.level.content.objects.CommonNote;
import han.chensing.latos.maker.windows.widget.CUIList;

import java.util.Objects;

public class NoteParser {
    public static String infoFromNote(CommonNote commonNote){
        return
                "distance="+ commonNote.getDistanceToLineCenter()
                +";\ntype="+ commonNote.getNoteType().name()
                +";\nshown="+ commonNote.isShown()
                +";\nbackGoing="+ commonNote.isBackGoing()+";";
    }

    public static String infoFromNotes(OrderedSet<CUIList.CUIListItem<CommonNote>> notes){
        String  distance=null;
        String  noteType=null;
        String  shown=null;
        String  backGoing=null;
        boolean isFirst=true;
        for (CUIList.CUIListItem<CommonNote> noteCUIListItem : notes) {
            CommonNote commonNote = noteCUIListItem.getValue();
            String eDistance = commonNote.getDistanceToLineCenter() + "";
            String eNoteType = commonNote.getNoteType() + "";
            String eShown = commonNote.isShown() + "";
            String eBackGoing = commonNote.isBackGoing() + "";
            if (isFirst) {
                distance = eDistance;
                noteType = eNoteType;
                shown = eShown;
                backGoing = eBackGoing;
                isFirst=false;
                continue;
            }
            if (!Objects.equals(eDistance, distance))
                distance = "...";
            if (!Objects.equals(eNoteType, noteType))
                noteType = "...";
            if (!Objects.equals(eShown, shown))
                shown = "...";
            if (!Objects.equals(eBackGoing, backGoing))
                backGoing = "...";
        }
        return String.format("distance=%s;\ntype=%s;\nshown=%s;\nbackGoing=%s;",distance,noteType,shown,backGoing);
    }

    public static void infoToNotes(OrderedSet<CUIList.CUIListItem<CommonNote>> notes, String info){
        try{
            String replace = info.replace("\n", "");
            String[] paras = replace.split(";");
            for(CUIList.CUIListItem<CommonNote> noteCUIListItem :notes) {
                CommonNote commonNote = noteCUIListItem.getValue();
                for (String para : paras) {
                    String[] kv = para.split("=");
                    String s = kv[1];
                    String key = kv[0];
                    if (s.equals("..."))
                        continue;
                    updateSingleNote(commonNote, s, key);
                }
            }
        }catch (Exception ignore){}
    }

    public static void infoToNote(CommonNote commonNote, String info){
        try {
            String replace = info.replace("\n", "");
            String[] paras = replace.split(";");
            for(String para:paras) {
                String[] kv = para.split("=");
                String s = kv[1];
                String key = kv[0];
                updateSingleNote(commonNote, s, key);
            }
        }catch (Exception ignore){}
    }

    private static void updateSingleNote(CommonNote commonNote, String s, String key) {
        switch (key) {
            case "distance":
                commonNote.setDistanceToLineCenter(Integer.parseInt(s));
                break;
            case "type":
                commonNote.setNoteType(CommonNote.NoteType.valueOf(s));
                break;
            case "shown":
                commonNote.setShown(Boolean.parseBoolean(s));
                break;
            case "backGoing":
                commonNote.setBackGoing(Boolean.parseBoolean(s));
                break;
        }
    }
}
